<?
/***********************************************************************************************
Created by : M.Ansar Basha
[created : 02/23/2007, 11:33:47 AM]
 Description:Bank Realization Certificate Entry Page.
/***********************************************************************************************/

include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/global_form.inc");//
include(DOCINCLUDES."global_files/global_report.inc");
include(DOCINCLUDES."global_files/tables.inc");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_session.inc");


$page=new cPage;
$page->header_path="../../";

$form=new cForm();
$db=new cDatabase($company_id);
$report=new cReport();
$sql=new pgSql();//

if($_GET)
{
$brc_id=$_GET['brc_id'];
$action=$_GET['action'];
$invoice_no=$_GET['invoice_no'];
}
else
{
$action=$_POST['action'];
$brc_id=$_POST['brc_id'];

}

if($_POST)
{
	$db->beginTransaction();

	$brc_commercial_array["transaction_id"]=$_POST['brc_id_duplicate'];
	$brc_commercial_array["transaction_type"]=$_POST['invoice_type_duplicate'];
	$brc_commercial_array["description"]=$_POST['description'];
	$brc_commercial_array["export_promotion"]=$_POST['exp_promotion_no'];
 	$brc_commercial_array["export_promotion_date"]=getFormattedDate($_POST['export_promotion_date']);
	$brc_commercial_array["bill_of_lading"]=$_POST['bill_of_lading_no'];
	$brc_commercial_array["bill_of_lading_date"]=getFormattedDate($_POST['bill_of_lading_date']);
	$brc_commercial_array["value1"]=$_POST['bill_amount1'];
	$brc_commercial_array["value2"]=$_POST['bill_amount2'];
	$brc_commercial_array["bill_amount"]=$_POST['bill_amount_duplicate'];
	$brc_commercial_array["freight_amount"]=$_POST['frieght'];
	$brc_commercial_array["insurance_amount"]=$_POST['insurence'];
	$brc_commercial_array["commission_paid"]=$_POST['commision'];
	$brc_commercial_array["convertable"]=$_POST['curr_convertable'];
	$brc_commercial_array["fob_value"]=$_POST['fob_value'];
	$brc_commercial_array["bank_ref_no"]=$_POST['bank_ref'];
	$brc_commercial_array["date_of_export"]=getFormattedDate($_POST['bank_reference_date']);//
	$brc_commercial_array["date_of_realization"]=getFormattedDate($_POST['date_of_realization']);//
	$brc_commercial_array["form_no"]=$_POST['form_no'];//
	$brc_commercial_array["category_id"]=$_POST['licence'];//
	$brc_commercial_array["sign_name"]=$_POST['exporter_name'];//
	$brc_commercial_array["designation"]=$_POST['designation'];//

	$sql->table = $brc;
	$sql->column = $brc_commercial_array;
	if($action=='edit')
	{
		$condition_array['brc_id']="brc_id=".$brc_id;
  		$sql->condition =$condition_array;
  		$sql->generateUpdateQuery();
	}
	else
	{
		$sql->generateInsertQuery();
	}
	 $db->query = $sql->query;
	 $db->runQuery();

	if($action!='edit')
	{
		$db->query="Select max(brc_id) as brc_id from $brc;";
		$db->runQuery();
		$id_array=$db->returnArrays();
		$brc_id=$id_array[0]['brc_id'];
	}

	$db->endTransaction();
	unset($_POST);
	header("location:brc_details.php?brc_id=$brc_id");
	exit;
}	
if($action=="edit")
$title="Edit Page for Bank Realization Certificate no - $brc_id";
else
$title="Bank Realization Certificate Entry Page";

$page->title=$page->heading="$title";
$page->doHeaderHtml();

$form->properties=array("name"=>"brcform","method"=>"post","action"=>$_SERVER
["PHP_SELF"]);
echo $form->startForm();
$brc_commercial_details_array=array();
 if($action=='edit')
{
$db->query="select brc_id, transaction_id, transaction_type, description,export_promotion, to_char(export_promotion_date,'dd/mm/yyyy') as export_promotion_date, bill_of_lading, to_char(bill_of_lading_date,'dd/mm/yyyy') as bill_of_lading_date, value1, value2, bill_amount, freight_amount, insurance_amount, commission_paid, convertable, fob_value, bank_ref_no, to_char(date_of_export,'dd/mm/yyyy') as date_of_export, to_char(date_of_realization,'dd/mm/yyyy') as date_of_realization, form_no, category_id, sign_name, designation,invoice_no from brc_view where brc_id=$brc_id;";
$db->runQuery();

$brc_details_array=$db->returnArrays();
$invoice_no=$brc_details_array[0]['transaction_id']."-".$brc_details_array[0]['transaction_type'];

$db->query="Select gsp_id,1,invoice_no from invoice_details where gsp_id in (SELECT transaction_id||'-'||transaction_type From brc where brc_id=$brc_id)";
$db->runQuery();
$invoice_no_array=$db->returnArrays();

}
else
{
$db->query="select gsp_id,1,invoice_no from invoice_details;";
$db->runQuery();
$invoice_no_array=$db->returnArrays();
}


$form->properties=array("name"=>"invoice","class"=>"textm","onChange"=>"self.location.href=\"brc.php?invoice_no=\"+this.value");
$form->select_tag_properties=array("default"=>$invoice_no,"value"=>$_GET['invoice'],"arrays"=>$invoice_no_array);

$brcform_data[0]["Select Invoice Number"]=$form->generateSelectTag();

if($invoice_no)
{

$db->query="select invoice_no from invoice_details where gsp_id='".$invoice_no."';";
$db->runQuery();
$invoice_date_array=$db->returnArrays();

 $invoice_date=explode('/',$invoice_date_array[0]['invoice_no']);

$brcform_data[0]["Invoice Date"]=$invoice_date[1];

$inv_no= explode('-',$invoice_no);

$form->properties=
array("name"=>"invoice_type_duplicate","value"=>$invoice_date[2],"type"=>"hidden");
echo $form->generateInputTag();




if (trim($inv_no[1])=="gsp")
{
	 $db->query="select country_to,gsp_id from $gsp_commercial_details_view WHERE gsp_id='$inv_no[0]';";
}
elseif (trim($inv_no[1])=="coc")
{
	 $db->query="select destination as \"country_to\", commercial_id as \"gsp_id\" from $certificate_of_origin_view WHERE commercial_id='$inv_no[0]';";
}
$db->runQuery();

$country_to_array=$db->returnArrays();
$brcform_data[0]["Destination of Goods"]=$country_to_array[0]['country_to'];

$brcform_id=$country_to_array[0]['gsp_id'];

$form->properties=
array("name"=>"brc_id_duplicate","value"=>$brcform_id,"type"=>"hidden");
echo $form->generateInputTag();

if ($action=='edit')
	$exp_promotion_no=$brc_details_array[0]['export_promotion'];
$form->properties =
array("name"=>"exp_promotion_no","value"=>$exp_promotion_no,"size"=>"30",
"maxlength"=>"250","class"=>"textn");
$brcform_data[0]["Export Promotion No"]=$form->generateInputTag();
$brc_item_details_array=array();

if ($action=='edit')
	$exp_promotion_date=$brc_details_array[0]['export_promotion_date'];
else
	$exp_promotion_date=date("d/m/Y");


$form->properties=array("name"=>"export_promotion_date","type"=>"text","value"=>
$exp_promotion_date,"size"=>"9","maxlength"=>"10","class"=>"datem");
$brcform_data[0]["Export Promotion Date (dd/mm/yyyy)"]=$form->generateInputTag();

$brcform_data[0]["Export Promotion Date (dd/mm/yyyy)"].="<a
href=\"javascript:show_calendar('brcform.export_promotion_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"".$page->header_path."images/calendar.gif\" width=\"16\"
height=\"16\" border=\"0\"></a>";

$brcform_data[0]["Export Promotion Date (dd/mm/yyyy)"].=$dv->errors["issue_date"];

if ($action=='edit')
	$exp_description=$brc_details_array[0]['description'];
$form->properties =
array("name"=>"description","value"=>$exp_description,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$brcform_data[0]["Description"]=$form->generateInputTag();

if ($action=='edit')
	$bill_of_lading =$brc_details_array[0]['bill_of_lading'];

$form->properties =
array("name"=>"bill_of_lading_no","value"=>$bill_of_lading,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$brcform_data[0]["Bill of Lading No."]=$form->generateInputTag();

if ($action=='edit')
	$bill_of_lading_date=$brc_details_array[0]['bill_of_lading_date'];

if ($action=='edit')
{
	$bill_of_lading_date=$brc_details_array[0]['bill_of_lading_date'];
}
else
{
	$bill_of_lading_date=date("d/m/Y");

}
$form->properties=array("name"=>"bill_of_lading_date","type"=>"text","value"=>
$bill_of_lading_date,"size"=>"9","maxlength"=>"10","class"=>"datem");
$brcform_data[0]["Bill of Lading Date (dd/mm/yyyy)"]=$form->generateInputTag();

$brcform_data[0]["Bill of Lading Date (dd/mm/yyyy)"].="<a
href=\"javascript:show_calendar('brcform.bill_of_lading_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"".$page->header_path."images/calendar.gif\" width=\"16\"
height=\"16\" border=\"0\"></a>";

$brcform_data[0]["Bill of Lading Date (dd/mm/yyyy)"].=$dv->errors["issue_date"];

if ($action=='edit')
	$bill_amount1=$brc_details_array[0]['value1'];
$form->properties =
array("name"=>"bill_amount1","value"=>$bill_amount1,"size"=>"30",
"maxlength"=>"350","class"=>"textn","onChange"=>"calculatetotal();");
$brcform_data[0]["Value1"]=$form->generateInputTag();

if ($action=='edit')
	$bill_amount2=$brc_details_array[0]['value2'];
$form->properties =
array("name"=>"bill_amount2","value"=>$bill_amount2,"size"=>"30",
"maxlength"=>"350","class"=>"textn","onChange"=>"calculatetotal();");
$brcform_data[0]["Value2"]=$form->generateInputTag();

if ($action=='edit')
	$bill_amount=$brc_details_array[0]['bill_amount'];
$form->properties =
array("name"=>"bill_amount","value"=>$bill_amount,"size"=>"30",
"maxlength"=>"350","class"=>"textn","disabled"=>"true");
$brcform_data[0]["Bill Amount"]=$form->generateInputTag();

$form->properties=
array("name"=>"bill_amount_duplicate","value"=>0,"type"=>"hidden");
echo $form->generateInputTag();

if ($action=='edit')
	$frieght=$brc_details_array[0]['freight_amount'];
$form->properties =
array("name"=>"frieght","value"=>$frieght,"size"=>"50",
"maxlength"=>"350","class"=>"textn","onChange"=>"calculatefob();");
$brcform_data[0]["Freight Charges"]=$form->generateInputTag();

if ($action=='edit')
	$insurence=$brc_details_array[0]['insurance_amount'];
$form->properties =
array("name"=>"insurence","value"=>$insurence,"size"=>"50",
"maxlength"=>"350","class"=>"textn","onChange"=>"calculatefob();");
$brcform_data[0]["Insurence Amount"]=$form->generateInputTag();

if ($action=='edit')
	$commision=$brc_details_array[0]['commission_paid'];
$form->properties =
array("name"=>"commision","value"=>$commision,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$brcform_data[0]["Commision"]=$form->generateInputTag();

$form_no_array=array("0"=>array("YES"),"1"=>array("NO"));

if ($action=='edit')
	$curr_conv=$brc_details_array[0]['convertable'];
$form->properties=array("name"=>"curr_convertable");
$form->select_tag_properties=array("default"=>$curr_conv,"arrays"=>$form_no_array,"restore_default"=>$_POST[
"curr_convertable"]);
$brcform_data[0]["Whether the export is in freely convertable currency or in INR"]=$form->generateSelectTag();

if ($action=='edit')
	$fob_val=$brc_details_array[0]['fob_value'];
else
	$fob_val=0;
$form->properties=
array("name"=>"fob_value","value"=>$fob_val,"type"=>"hidden");
echo $form->generateInputTag();

$form->properties =
array("name"=>"fob_value_duplicate","value"=>$fob_val,"size"=>"20",
"maxlength"=>"10","class"=>"textn");
$brcform_data[0]["FOB Value"]=$form->generateInputTag();

if ($action=='edit')
	$bank_ref=$brc_details_array[0]['bank_ref_no'];
$form->properties =
array("name"=>"bank_ref","value"=>$bank_ref,"size"=>"20",
"maxlength"=>"10","class"=>"textn");
$brcform_data[0]["Bank Reference No."]=$form->generateInputTag();

if ($action=='edit')
	$bank_ref_date=$brc_details_array[0]['date_of_export'];
else
	$bank_ref_date=date('d/m/Y');


$form->properties=array("name"=>"bank_reference_date","type"=>"text","value"=>
$bank_ref_date,"size"=>"9","maxlength"=>"10","class"=>"datem");
$brcform_data[0]["Bank Reference Date (dd/mm/yyyy)"]=$form->generateInputTag();

$brcform_data[0]["Bank Reference Date (dd/mm/yyyy)"].="<a
href=\"javascript:show_calendar('brcform.bank_reference_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"".$page->header_path."images/calendar.gif\" width=\"16\"
height=\"16\" border=\"0\"></a>";


if ($action=='edit')
	$date_of_realization=$brc_details_array[0]['date_of_realization'];
else
	$date_of_realization=date('d/m/Y');
$form->properties=array("name"=>"date_of_realization","type"=>"text","value"=> 
$date_of_realization,"size"=>"9","maxlength"=>"10","class"=>"datem");
$brcform_data[0]["Date of Realization (dd/mm/yyyy)"]=$form->generateInputTag();

$brcform_data[0]["Date of Realization (dd/mm/yyyy)"].="<a
href=\"javascript:show_calendar('brcform.date_of_realization');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"".$page->header_path."images/calendar.gif\" width=\"16\"
height=\"16\" border=\"0\"></a>";

$brcform_data[0]["Bill of Lading Date (dd/mm/yyyy)"].=$dv->errors["issue_date"];

$form_no_array=array("0"=>array("SDF"),"1"=>array("GRI"),"2"=>array("PP"));
if ($action=='edit')
	$form_no_value=$brc_details_array[0]['form_no'];

$form->properties=array("name"=>"form_no");
$form->select_tag_properties=array("default"=>$form_no_value,"arrays"=>$form_no_array,"restore_default"=>$_POST[
"form_no"]);
$brcform_data[0]["SDF/GRI/PP - Form No"]=$form->generateSelectTag();

if ($action=='edit')
	$cat_val=$brc_details_array[0]['category_id'];
else
	$cat_val="DFCE LICENCE";
$form->properties =
array("name"=>"licence","value"=>$cat_val,"size"=>"20",
"maxlength"=>"15","class"=>"textn");
$brcform_data[0]["No.Date & Catagory of Applicable Licence."]=$form->generateInputTag();

if ($action=='edit')
	$exporter_name=$brc_details_array[0]['sign_name'];
$form->properties =
array("name"=>"exporter_name","value"=>$exporter_name,"size"=>"20",
"maxlength"=>"10","class"=>"textn");
$brcform_data[0]["Name"]=$form->generateInputTag();

if ($action=='edit')
	$dsgn=$brc_details_array[0]['designation'];
$form->properties =
array("name"=>"designation","value"=>$dsgn,"size"=>"20",
"maxlength"=>"10","class"=>"textn");
$brcform_data[0]["Designation"]=$form->generateInputTag();


$brc_item_details_array=array();

if($action=='edit')
$button_text="Edit";
else
$button_text="Insert";
$form->properties=array("type"=>"button","name"=>"insert","value"=>"$button_text",
"onClick"=>"showConfirmBox('brc.php','certificate_of_originform');");
$brc_save[0][""]="<BR>".$form->generateInputTag();


$form->properties=array("type"=>"hidden","name"=>"brc_id","value"=>"$brc_id");
echo $form->generateInputTag();
$form->properties=array("type"=>"hidden","name"=>"action","value"=>"$action");
echo $form->generateInputTag();

}

$report->data=$brcform_data;
$report->title="$title";
$report->customization["table_style"]="width:40%;";
echo "<div align=\"center\">";
$report->doVerticalTable();



$report->data=$brcitem_data;
$report->title="";
$report->customization["table_style"]="width:50%;
backcolour:white;border-style:none; border-width:none;
border-collapse:collapse";
$report->customization["tr_style"]="border-style:none;
border-width:thin; border-collapse:collapse";
$report->customization["column_style"][""]="text-align:center";
$report->highlight=true;
$report->doHorizontalTable();
echo "</div>";

$report->data=$brc_save;
$report->title="";
$report->customization["table_style"]="width:100%;
backcolour:white;border-style:none; border-width:none;
border-collapse:collapse";
$report->customization["tr_style"]="border-style:none;
border-width:none";

$report->highlight=False;
$report->doHorizontalTable();
echo $form->endForm();


?>


<script language="javascript">
function calculatetotal()
{
	document.brcform.bill_amount.value=document.brcform.bill_amount1.value*document.brcform.bill_amount2.value;
	document.brcform.bill_amount_duplicate.value=document.brcform.bill_amount1.value*document.brcform.bill_amount2.value;
}

function calculatefob()
{
	document.brcform.fob_value_duplicate.value=parseInt(document.brcform.bill_amount.value)-(parseInt(document.brcform.frieght.value) + parseInt(document.brcform.insurence.value));

	document.brcform.fob_value.value=parseInt(document.brcform.bill_amount.value)-(parseInt(document.brcform.frieght.value) + parseInt(document.brcform.insurence.value));

}	

function showConfirmBox(file_name,form_name)
{
var ok = confirm("Do you want to save");
if(ok)
{
document.brcform.action="brc.php";
document.brcform.submit();
}
}
</script>
